package main

import (
	"fmt"
	"os"

	"gitee.com/libbylg/pkgr/repo"
)

var VERSION = "1.0.1"

func CmdHelp(argv []string) int {
	text := "" +
		"Usage:\n" +
		"    pkgr help|-h|--help\n" +
		"    pkgr version|-v|--version\n" +
		"    pkgr env\n" +
		"\n" +
		"    pkgr install   LABEL\n" +
		"    pkgr install   PLAN-FILE PLAN-VIEW\n" +
		"    pkgr uninstall LABEL\n" +
		"    pkgr list      [PATTERN] [--regex] [--plan PLAN-FILE] [--view PLAN-VIEW] [--devrepo|--pkgrepo] [--csv [SEP]] [--no-header]\n" +
		"    pkgr download  [PATTERN] [--regex] [--plan PLAN-FILE] [--view PLAN-VIEW] [--devrepo|--pkgrepo]\n" +
		"    pkgr dependent [PATTERN] [--regex] [--plan PLAN-FILE] [--view PLAN-VIEW] [--devrepo|--pkgrepo] [--tree|--list]\n" +
		"    pkgr rehash    [PATTERN] [--regex] [--plan PLAN-FILE] [--view PLAN-VIEW] [--devrepo|--pkgrepo] [--writeback]\n" +
		"    pkgr selection [PATTERN] [--regex] [--plan PLAN-FILE] [--view PLAN-VIEW] [--devrepo|--pkgrepo]\n" +
		"                   [--order ORDER-TAG:ORDER-FILE ...] [--separator|-s SEP] [--sort] [--no-header]\n" +
		"    pkgr query     LABEL [KEYWORDS|*|all]\n" +
		"    pkgr initenv   PLAN-FILE [REPO-DIR]\n" +
		"    pkgr merge     PLAN-FILE...\n" +
		"    pkgr encode    TOKEN\n" +
		"    pkgr decode    TOKEN\n" +
		"    pkgr export    LABEL [--table|--json]\n" +
		"    pkgr parse     LABEL [--table|--json]\n" +
		"\n" +
		"ENVIRONMENT:\n" +
		"    DEVREPO  开发仓库路径\n" +
		"    PKGREPO  软件仓库路径\n" +
		"\n" +
		"KEYWORDS:\n" +
		"    l     LABEL\n" +
		"    n     MODULENAME\n" +
		"    v     MODULEVERSION\n" +
		"    p     PACKAGE\n" +
		"    st    STATUS\n" +
		"    desc  DESCCRIPTION\n" +
		"    lic   LICENSE\n" +
		"    imp   IMPORTAS\n" +
		"    req   REQUIRED\n" +
		"    tv    TRAINVERSION\n" +
		"    tmn   TRAINMODULENAME\n" +
		"    tmv   TRAINMODULEVERSION\n" +
		""
	fmt.Print(text)
	return 0
}

func CmdVersion(argv []string) int {
	fmt.Printf("%v\n", VERSION)
	return 0
}

func CmdEnv(argv []string) int {
	fmt.Printf("PKGREPO=%v\n", repo.PKGREPO)
	fmt.Printf("DEVREPO=%v\n", repo.DEVREPO)
	return 0
}

func CmdMain(argv []string) int {
	if len(argv) <= 1 {
		ERROR("Missing parameters, try -h for help")
		return 1
	}

	repo.DEVREPO = os.Getenv("DEVREPO")
	repo.PKGREPO = os.Getenv("PKGREPO")

	if argv[1] == "help" || argv[1] == "-h" || argv[1] == "--help" {
		return CmdHelp(argv)
	}

	if argv[1] == "version" || argv[1] == "-v" || argv[1] == "--version" {
		return CmdVersion(argv)
	}

	if argv[1] == "env" {
		return CmdEnv(argv)
	}

	if argv[1] == "list" {
		return CmdList(argv)
	}

	if argv[1] == "dependent" {
		return CmdDependent(argv)
	}

	if argv[1] == "download" {
		return CmdDownload(argv)
	}

	if argv[1] == "rehash" {
		return CmdRehash(argv)
	}

	if argv[1] == "selection" {
		return CmdSelection(argv)
	}

	if argv[1] == "install" {
		return CmdInstall(argv)
	}

	if argv[1] == "uninstall" {
		return CmdUninstall(argv)
	}

	if argv[1] == "query" {
		return CmdQuery(argv)
	}

	if argv[1] == "merge" {
		return CmdMerge(argv)
	}

	if argv[1] == "initenv" {
		return CmdProxy(argv)
	}

	if argv[1] == "encode" {
		return CmdEncode(argv)
	}

	if argv[1] == "decode" {
		return CmdDecode(argv)
	}

	if argv[1] == "parse" {
		return CmdParse(argv)
	}

	if argv[1] == "export" {
		return CmdExport(argv)
	}

	return CmdProxy(argv)
}

func main() {
	os.Exit(CmdMain(os.Args))
}
